package com.yequan.leeetcoden;

/**
 * @author yequan
 * @date 2021/06/13 14:42
 **/
public class MaxArea {

    public static void main(String[] args) {
        int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        System.out.println(maxArea2(height));
    }

    public static int maxArea(int[] heights) {
        if (null == heights || heights.length < 2) {
            return 0;
        }
        int left = 0;
        int right = heights.length - 1;
        int maxArea = 0;
        while (left < right) {
            maxArea = heights[right] < heights[left]
                    ? Math.max(maxArea, heights[left++] * (right - left))
                    : Math.max(maxArea, heights[right--] * (right - left));
        }
        return maxArea;
    }

    public static int maxArea2(int[] heights) {
        if (null == heights || heights.length < 2) {
            return 0;
        }
        int left = 0;
        int right = heights.length - 1;
        int maxArea = 0;
        while (left < right) {
            maxArea = heights[left] < heights[right]
                    ? Math.max(maxArea, (right - left) * heights[left++])
                    : Math.max(maxArea, (right - left) * heights[right--]);
        }
        return maxArea;
    }

}
